#include "optionsdialog.h"

OptionsDialog::OptionsDialog(QWidget *parent):
    QDialog(parent),
    max_alarms(8),
    audio_dir_name("audio")
{
    setStyleSheet("QLabel {font-size: 12px}");
    setWindowTitle("WebAlarmClient Options");
    mainLayout = new QHBoxLayout();
    infoLayout = new QGridLayout();
    toolsLayout = new QVBoxLayout();
    advancedLayout = new QVBoxLayout();

    infoGroupBox = new QGroupBox("General",this);
    advancedGroupBox = new QGroupBox("Advanced",this);

    enableCheckBox = new QCheckBox*[max_alarms];
    nameLineEdit = new QLineEdit*[max_alarms];
    audioComboBox = new QComboBox*[max_alarms];
    for(int i = 0; i < max_alarms; ++i){
        enableCheckBox[i] = new QCheckBox(this);
        nameLineEdit[i] = new QLineEdit(this);
        nameLineEdit[i]->setMinimumWidth(200);
        audioComboBox[i] = new QComboBox(this);
        audioComboBox[i]->setMinimumWidth(100);
        audioComboBox[i]->setMaximumWidth(150);

        infoLayout->addWidget(enableCheckBox[i],i,1);
        infoLayout->addWidget(nameLineEdit[i],i,2);
        infoLayout->addWidget(audioComboBox[i],i,3);
        QObject::connect(enableCheckBox[i],SIGNAL(clicked(bool)),nameLineEdit[i],SLOT(setEnabled(bool)));
        QObject::connect(enableCheckBox[i],SIGNAL(clicked()),nameLineEdit[i],SLOT(setFocus()));
    }
    infoGroupBox->setLayout(infoLayout);


    ipChangeDialog = new IPChangeDialog(this);
    ipChangeDialog->setModal(true);

    ipChangeButton = new QPushButton("Change IP",this);
    QObject::connect(ipChangeButton,SIGNAL(clicked()),ipChangeDialog,SLOT(show()));
    QObject::connect(ipChangeDialog,SIGNAL(accepted()),this,SLOT(changeIP()));
    hostIPLineEdit = new QLineEdit(this);
    QRegExp ipRegExp("^((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))");
    QRegExpValidator* ipValidator = new QRegExpValidator(ipRegExp,hostIPLineEdit);
    hostIPLineEdit->setValidator(ipValidator);
    QLabel* l = new QLabel("Server IP address:",this);
    l->setAlignment(Qt::AlignCenter);
    l->setBuddy(hostIPLineEdit);
    errorComboBox = new QComboBox(this);
    QLabel* e = new QLabel("Error sound:",this);
    e->setAlignment(Qt::AlignCenter);
    e->setBuddy(errorComboBox);
    okButton = new QPushButton("OK",this);
    cancelButton = new QPushButton("Cancel",this);
    QObject::connect(okButton,SIGNAL(clicked()),this,SLOT(saveAccepted()));
    QObject::connect(cancelButton,SIGNAL(clicked()),this,SLOT(saveRejected()));
    advancedLayout->addWidget(ipChangeButton);
    advancedLayout->addWidget(l);
    advancedLayout->addWidget(hostIPLineEdit);
    advancedLayout->addWidget(e);
    advancedLayout->addWidget(errorComboBox);
    advancedGroupBox->setLayout(advancedLayout);
    toolsLayout->addWidget(advancedGroupBox);
    toolsLayout->addStretch();
    toolsLayout->addWidget(okButton);
    toolsLayout->addWidget(cancelButton);
    mainLayout->addWidget(infoGroupBox);
    mainLayout->addLayout(toolsLayout);
    loadSettings();
    setLayout(mainLayout);
    setFixedSize(sizeHint());
}

void OptionsDialog::changeIP()
{
    emit ipChanged(ipChangeDialog->getIP(),ipChangeDialog->getMask());
    //qDebug("Ip changed");
}

void OptionsDialog::saveAccepted()
{
    QSettings settings("ShH Soft","WebAlarmClient");

    settings.beginGroup("Alarms");
    for(int i = 0; i < max_alarms; ++ i){
        settings.setValue(QString("enableCheckBox%1").arg(i),enableCheckBox[i]->isChecked());
        settings.setValue(QString("nameLineEdit%1").arg(i),nameLineEdit[i]->text());
        settings.setValue(QString("audio%1").arg(i),audioComboBox[i]->currentText());
    }
    settings.endGroup();
    settings.setValue(QString("errorAudio"),errorComboBox->currentText());
    settings.setValue("hostIP",hostIPLineEdit->text());
    this->accept();
}

void OptionsDialog::loadSettings()
{
    QDir dir;
    QStringList filters;
    filters<<"*.mp3"<<"*.wav"<<"*.ogg";
    dir.setNameFilters(filters);
    if(!dir.exists(audio_dir_name)){
        dir.mkdir(audio_dir_name);
    }
    else {
        dir.cd(audio_dir_name);
    }
    QStringList files = dir.entryList(QDir::Files);
    files.push_front("<No Sound>");
    QSettings settings("ShH Soft","WebAlarmClient");

    settings.beginGroup("Alarms");
    for(int i = 0; i < max_alarms; ++i){
        enableCheckBox[i]->setChecked(settings.value(QString("enableCheckBox%1").arg(i)).toBool());
        nameLineEdit[i]->setEnabled(enableCheckBox[i]->isChecked());
        nameLineEdit[i]->setText(settings.value(QString("nameLineEdit%1").arg(i)).toString());
        audioComboBox[i]->clear();
        audioComboBox[i]->addItems(files);
        QString a = settings.value(QString("audio%1").arg(i)).toString();
        int c;
        if((c = audioComboBox[i]->findText(a)) != -1){
            audioComboBox[i]->setCurrentIndex(c);
        }
    }
    settings.endGroup();
    errorComboBox->clear();
    errorComboBox->addItems(files);
    QString a = settings.value(QString("errorAudio")).toString();
    int c;
    if((c = errorComboBox->findText(a)) != -1){
        errorComboBox->setCurrentIndex(c);
    }
    hostIPLineEdit->setText(settings.value("hostIP").toString());

}

void OptionsDialog::saveRejected()
{
    loadSettings();
    this->reject();
}
